﻿namespace NEvoWeb.Modules.NB_Store
{
    using DotNetNuke.Common;
    using DotNetNuke.Common.Utilities;
    using DotNetNuke.Framework;
    using DotNetNuke.Services.Exceptions;
    using DotNetNuke.Services.Localization;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.IO;
    using System.Runtime.CompilerServices;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class AdminProductDoc : UserControlBase
    {
        [AccessedThroughProperty("cmdAddDoc")]
        private LinkButton _cmdAddDoc;
        [AccessedThroughProperty("cmdBrowse")]
        private FileUpload _cmdBrowse;
        [AccessedThroughProperty("cmdSearch")]
        private LinkButton _cmdSearch;
        [AccessedThroughProperty("dgDocs")]
        private DataGrid _dgDocs;
        [AccessedThroughProperty("dgSelectDoc")]
        private DataGrid _dgSelectDoc;
        [AccessedThroughProperty("lblBrowse")]
        private Label _lblBrowse;
        [AccessedThroughProperty("lblMsg")]
        private Label _lblMsg;
        [AccessedThroughProperty("RegularExpressionValidator1")]
        private RegularExpressionValidator _RegularExpressionValidator1;
        [AccessedThroughProperty("txtSearch")]
        private TextBox _txtSearch;
        private string ResourceFile;

        public event AddButtonEventHandler AddButton;

        public event DeleteDocEventHandler DeleteDoc;

        public event SearchDocsEventHandler SearchDocs;

        public event SelectDocEventHandler SelectDoc;

        private void cmdAddDoc_Click(object sender, EventArgs e)
        {
            
            if (AddButton != null)
            {
                AddButton(RuntimeHelpers.GetObjectValue(sender), e);
            }
        }

        private void cmdSearch_Click(object sender, EventArgs e)
        {
            this.dgSelectDoc.CurrentPageIndex = 0;

            if (SearchDocs != null)
            {
                SearchDocs(this.txtSearch.Text);
            }
        }

        private void dgDocs_DeleteCommand(object source, DataGridCommandEventArgs e)
        {
            try
            {
                DataGridItem item = e.Item;
                int docID = int.Parse(e.CommandArgument.ToString());
                new ProductController().DeleteProductDoc(docID);
                
                if (DeleteDoc != null)
                {
                    DeleteDoc(docID);
                }
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exc = exception1;
                DotNetNuke.Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
                ProjectData.ClearProjectError();
            }
        }

        private void dgDocs_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            DataGridItem item = e.Item;
            if (((item.ItemType == ListItemType.Item) | (item.ItemType == ListItemType.AlternatingItem)) | (item.ItemType == ListItemType.SelectedItem))
            {
                Control control = item.Controls[1].Controls[0];
                if (control is ImageButton)
                {
                    ImageButton button = (ImageButton) control;
                    button.Attributes.Add("onClick", "javascript:return confirm('" + DotNetNuke.Services.Localization.Localization.GetString("cmdDeleteDoc", this.ResourceFile) + "');");
                }
            }
        }

        private void dgDocs_PreRender(object sender, EventArgs e)
        {
            DataGrid grid = (DataGrid) sender;
            this.ResourceFile = DotNetNuke.Services.Localization.Localization.GetResourceFile(this, this.GetType().BaseType.Name + ".ascx");
            if (grid.Controls.Count > 0)
            {
                ((ShowSelectLang) grid.Controls[0].Controls[0].Controls[0].FindControl("ShowSelectLang")).Refresh();
                ((Label) grid.Controls[0].Controls[0].Controls[0].FindControl("nlName")).Text = DotNetNuke.Services.Localization.Localization.GetString("nlName", this.ResourceFile);
                ((ShowSelectLang) grid.Controls[0].Controls[0].Controls[0].FindControl("ShowSelectLang2")).Refresh();
                ((Label) grid.Controls[0].Controls[0].Controls[0].FindControl("nlDescription")).Text = DotNetNuke.Services.Localization.Localization.GetString("nlDescription", this.ResourceFile);
            }
        }

        private void dgSelectDoc_EditCommand(object source, DataGridCommandEventArgs e)
        {
            try
            {
                
                if (SelectDoc != null)
                {
                    SelectDoc(e.Item.Cells[1].Text, e.Item.Cells[2].Text, e.Item.Cells[3].Text, e.Item.Cells[4].Text);
                }
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exc = exception1;
                DotNetNuke.Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
                ProjectData.ClearProjectError();
            }
        }

        private void dgSelectDoc_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            this.dgSelectDoc.CurrentPageIndex = e.NewPageIndex;
            
            if (SearchDocs != null)
            {
                SearchDocs(this.txtSearch.Text);
            }
        }

        public int DocsCount()
        {
            return this.dgDocs.Items.Count;
        }

        public void HideSelectList()
        {
            this.dgSelectDoc.Visible = false;
        }

        public void populateDocs(int ProductID, string Lang)
        {
            ProductController controller = new ProductController();
            this.ResourceFile = DotNetNuke.Services.Localization.Localization.GetResourceFile(this, this.GetType().BaseType.Name + ".ascx");
            DataGrid dgDocs = this.dgDocs;
            DotNetNuke.Services.Localization.Localization.LocalizeDataGrid(ref dgDocs, this.ResourceFile);
            this.dgDocs = dgDocs;
            ArrayList productDocList = controller.GetProductDocList(ProductID, Lang);
            this.dgDocs.DataSource = productDocList;
            this.dgDocs.DataBind();
        }

        public void populateSelectDocs(string Lang, string FilterText)
        {
            ProductController controller = new ProductController();
            this.ResourceFile = DotNetNuke.Services.Localization.Localization.GetResourceFile(this, this.GetType().BaseType.Name + ".ascx");
            DataGrid dgSelectDoc = this.dgSelectDoc;
            DotNetNuke.Services.Localization.Localization.LocalizeDataGrid(ref dgSelectDoc, this.ResourceFile);
            this.dgSelectDoc = dgSelectDoc;
            ArrayList list = controller.GetProductSelectDocList(Lang, FilterText, this.PortalSettings.PortalId);
            this.dgSelectDoc.DataSource = list;
            this.dgSelectDoc.DataBind();
            this.dgSelectDoc.Visible = true;
        }

        public void updateDocs(string Lang)
        {
            IEnumerator enumerator=null;
            ProductController controller = new ProductController();
            try
            {
                enumerator = this.dgDocs.Items.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    DataGridItem current = (DataGridItem) enumerator.Current;
                    int docID = Conversions.ToInteger(current.Cells[0].Text);
                    NB_Store_ProductDocInfo productDoc = controller.GetProductDoc(docID, Lang);
                    if (productDoc != null)
                    {
                        productDoc.DocDesc = ((TextBox) current.FindControl("txtDocDesc")).Text;
                        productDoc.ListOrder = Conversions.ToInteger(((TextBox) current.FindControl("txtListOrder")).Text);
                        productDoc.Hidden = ((CheckBox) current.FindControl("chkHide")).Checked;
                        productDoc.FileName = ((TextBox) current.FindControl("txtDocName")).Text;
                        productDoc.Lang = Lang;
                        controller.UpdateObjProductDoc(productDoc);
                    }
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
        }

        public void UploadDoc(int ProdID, string Lang)
        {
            FileSystemUtils utils = new FileSystemUtils();
            ProductController controller = new ProductController();
            if (this.cmdBrowse.FileName != "")
            {
                SharedFunctions.CreateDir(this.PortalSettings, "productdocs");
                string str = Guid.NewGuid().ToString();
                NB_Store_ProductDocInfo objInfo = new NB_Store_ProductDocInfo {
                    DocID = -1,
                    DocDesc = "",
                    DocPath = this.PortalSettings.HomeDirectoryMapPath + @"productdocs\" + str + Path.GetExtension(this.cmdBrowse.FileName),
                    FileExt = Path.GetExtension(this.cmdBrowse.FileName),
                    FileName = Path.GetFileNameWithoutExtension(this.cmdBrowse.FileName),
                    Hidden = false,
                    Lang = Lang,
                    ListOrder = 1,
                    ProductID = ProdID
                };
                string subFolderPath = Globals.GetSubFolderPath(objInfo.DocPath, this.PortalSettings.PortalId);
                FileSystemUtils.UploadFile(this.PortalSettings.HomeDirectoryMapPath + @"productdocs\", this.cmdBrowse.PostedFile, str + Path.GetExtension(this.cmdBrowse.FileName));
                if (FileSystemUtils.GetFolder(this.PortalSettings.PortalId, "productdocs").StorageLocation == 1)
                {
                    objInfo.DocPath = this.PortalSettings.HomeDirectoryMapPath + @"productdocs\" + str + Path.GetExtension(this.cmdBrowse.FileName) + ".resources";
                }
                controller.UpdateObjProductDoc(objInfo);
            }
        }

        protected virtual LinkButton cmdAddDoc
        {
            get
            {
                return this._cmdAddDoc;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                EventHandler handler = new EventHandler(this.cmdAddDoc_Click);
                if (this._cmdAddDoc != null)
                {
                    this._cmdAddDoc.Click -= handler;
                }
                this._cmdAddDoc = value;
                if (this._cmdAddDoc != null)
                {
                    this._cmdAddDoc.Click += handler;
                }
            }
        }

        protected virtual FileUpload cmdBrowse
        {
            get
            {
                return this._cmdBrowse;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._cmdBrowse = value;
            }
        }

        protected virtual LinkButton cmdSearch
        {
            get
            {
                return this._cmdSearch;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                EventHandler handler = new EventHandler(this.cmdSearch_Click);
                if (this._cmdSearch != null)
                {
                    this._cmdSearch.Click -= handler;
                }
                this._cmdSearch = value;
                if (this._cmdSearch != null)
                {
                    this._cmdSearch.Click += handler;
                }
            }
        }

        protected virtual DataGrid dgDocs
        {
            get
            {
                return this._dgDocs;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                EventHandler handler = new EventHandler(this.dgDocs_PreRender);
                DataGridItemEventHandler handler2 = new DataGridItemEventHandler(this.dgDocs_ItemDataBound);
                DataGridCommandEventHandler handler3 = new DataGridCommandEventHandler(this.dgDocs_DeleteCommand);
                if (this._dgDocs != null)
                {
                    this._dgDocs.PreRender -= handler;
                    this._dgDocs.ItemDataBound -= handler2;
                    this._dgDocs.DeleteCommand -= handler3;
                }
                this._dgDocs = value;
                if (this._dgDocs != null)
                {
                    this._dgDocs.PreRender += handler;
                    this._dgDocs.ItemDataBound += handler2;
                    this._dgDocs.DeleteCommand += handler3;
                }
            }
        }

        protected virtual DataGrid dgSelectDoc
        {
            get
            {
                return this._dgSelectDoc;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                DataGridPageChangedEventHandler handler = new DataGridPageChangedEventHandler(this.dgSelectDoc_PageIndexChanged);
                DataGridCommandEventHandler handler2 = new DataGridCommandEventHandler(this.dgSelectDoc_EditCommand);
                if (this._dgSelectDoc != null)
                {
                    this._dgSelectDoc.PageIndexChanged -= handler;
                    this._dgSelectDoc.EditCommand -= handler2;
                }
                this._dgSelectDoc = value;
                if (this._dgSelectDoc != null)
                {
                    this._dgSelectDoc.PageIndexChanged += handler;
                    this._dgSelectDoc.EditCommand += handler2;
                }
            }
        }

        protected virtual Label lblBrowse
        {
            get
            {
                return this._lblBrowse;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._lblBrowse = value;
            }
        }

        protected virtual Label lblMsg
        {
            get
            {
                return this._lblMsg;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._lblMsg = value;
            }
        }

        protected virtual RegularExpressionValidator RegularExpressionValidator1
        {
            get
            {
                return this._RegularExpressionValidator1;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._RegularExpressionValidator1 = value;
            }
        }

        protected virtual TextBox txtSearch
        {
            get
            {
                return this._txtSearch;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._txtSearch = value;
            }
        }

        public delegate void AddButtonEventHandler(object sender, EventArgs e);

        public delegate void DeleteDocEventHandler(int DocID);

        public delegate void SearchDocsEventHandler(string FilterText);

        public delegate void SelectDocEventHandler(string FileName, string DocDesc, string DocPath, string FileExt);
    }
}

